#include <stdlib.h>
#include <gtk/gtk.h>
#include <string.h>

GtkWidget *lockerList;

static void unlockAll (GtkWidget *wid, GtkWidget *win)
{
  GtkWidget *dialog = NULL;

  dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "All lockers have been unlocked!");
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

static void unlock (GtkWidget *wid, GtkWidget *win)
{
  GtkWidget *dialog = NULL;
  GtkListBoxRow * selectedRow = NULL;
  GtkWidget *child = NULL;
  selectedRow = gtk_list_box_get_selected_row(lockerList);

  if(selectedRow == NULL){
    dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Please select a locker!");
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
  }

  else{
    child = gtk_bin_get_child(selectedRow);
    const gchar* name = gtk_widget_get_name(child);
    const gchar* lblString = "GtkLabel";
    if(!g_ascii_strcasecmp(name,lblString)){
        const gchar* lblLocker = gtk_label_get_text(child);
        const gchar* tempStr = " has been unlocked";
        const gchar* strLocker = "locker ";
        GList* tempList = NULL;
        tempList = g_list_append(tempList,tempStr);
        GList* tempList1 = NULL;
        tempList1 = g_list_append(tempList1,strLocker);
        gchar* text = g_strconcat((gchar*) tempList1->data,lblLocker, (gchar*) tempList->data,NULL);
        dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, text);
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
    }

    else{
        dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Not a GtkLabel in GtkListBox");
        gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
    }
  }
}

static void createLocker (GtkWidget *listBox, char *lblText, int position)
{
  GtkWidget *label = NULL;
  label = gtk_label_new(lblText);
  gtk_list_box_insert(listBox,label,position);

}

gboolean filter_func (GtkListBoxRow *row, gpointer data)
{
  gchar *txtFilter = data;
  gint *len = strlen(txtFilter);
  GtkWidget *child;
  child = gtk_bin_get_child(row);
  const gchar* name = gtk_widget_get_name(child);
  const gchar* lblLocker = gtk_label_get_text(child);
  return !g_ascii_strncasecmp(lblLocker,txtFilter,len);
}

void filter(GtkEditable* edit) {
  GtkListBoxRow * currentRow = NULL;
  GtkWidget *child = NULL;
  gint i = 0;

  const gchar* content = gtk_entry_get_text( GTK_ENTRY(edit) );
  gtk_list_box_set_filter_func (lockerList, filter_func, content, NULL);
}

void name_insert_after (GtkEditable* edit,
                   gchar* new_text,
                   gint new_length,
                   gpointer position,
                   gpointer data)
{
  filter(edit);
}

void name_delete_after (GtkEditable* edit,
                   gint start_pos,
                   gint end_pos,
                   gpointer data)
{
  filter(edit);
}

int main (int argc, char *argv[])
{
  GtkWidget *win = NULL;
  GtkWidget *fixed = NULL;
  GtkWidget *btnUnlockAll = NULL;
  GtkWidget *btnUnlock = NULL;
  GtkWidget *btnClose = NULL;
  GtkWidget *lockerFilter = NULL;
  lockerList = NULL;

  /* Initialize GTK+ */
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  gtk_init (&argc, &argv);
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

  /* Create the main window */
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (win), 8);
  gtk_window_set_title (GTK_WINDOW (win), "test");
  gtk_window_fullscreen(win);
  gtk_widget_realize (win);
  g_signal_connect (win, "destroy", gtk_main_quit, NULL);

  fixed = gtk_fixed_new();
  gtk_container_add(GTK_CONTAINER(win), fixed);

  lockerFilter = gtk_entry_new();
  gtk_entry_set_text(lockerFilter , "Search for locker");
  g_signal_connect_after( lockerFilter, "insert-text", G_CALLBACK(name_insert_after), NULL );
  g_signal_connect_after( lockerFilter, "delete-text", G_CALLBACK(name_delete_after), NULL );
  gtk_fixed_put(GTK_FIXED(fixed), lockerFilter, 150, 50);
  gtk_widget_set_size_request(lockerFilter, 700, 35);

  lockerList = gtk_list_box_new();
  createLocker(lockerList,"0",0);
  createLocker(lockerList,"1",1);
  gtk_fixed_put(GTK_FIXED(fixed), lockerList, 150, 100);
  gtk_widget_set_size_request(lockerList, 700, 500);

  btnUnlock = gtk_button_new_with_label("Unlock");
  g_signal_connect (G_OBJECT (btnUnlock), "clicked", G_CALLBACK (unlock), (gpointer) win);
  gtk_fixed_put(GTK_FIXED(fixed), btnUnlock, 150, 665);
  gtk_widget_set_size_request(btnUnlock, 223, 50);

  btnUnlockAll = gtk_button_new_with_label("Unlock All Lockers");
  g_signal_connect (G_OBJECT (btnUnlockAll), "clicked", G_CALLBACK (unlockAll), (gpointer) win);
  gtk_fixed_put(GTK_FIXED(fixed), btnUnlockAll, 383, 665);
  gtk_widget_set_size_request(btnUnlockAll, 223, 50);

  btnClose = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
  g_signal_connect (btnClose, "clicked", gtk_main_quit, NULL);
  gtk_fixed_put(GTK_FIXED(fixed), btnClose, 616, 665);
  gtk_widget_set_size_request(btnClose, 223, 50);

  /* Enter the main loop */
  gtk_widget_show_all (win);
  gtk_main ();
  return 0;
}
